package com.example.administrator.xxnapp.tucao;

import android.content.Context;
import android.util.Log;

import com.example.administrator.xxnapp.bean.TuCaoBean;
import com.example.administrator.xxnapp.fragment.tucao.IModelTuCao;
import com.example.administrator.xxnapp.http.CustomStringCallback;
import com.example.administrator.xxnapp.http.HttpUrl;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

/**
 * Created by Went_Gone on 2018/5/2.
 */

public class SendTuCaoPresenter {
    private IViewSendTuCao mIView;
    private IModelSendTuCao mIModel;

    public SendTuCaoPresenter(IViewSendTuCao mIView, IModelSendTuCao mIModel) {
        this.mIView = mIView;
        this.mIModel = mIModel;
    }

    public void sendTuCao(final int userId, String content){
        TuCaoBean bean = new TuCaoBean();
        bean.setUserId(userId);
        bean.setContentString(content);
        mIModel.sendTuCao(bean)
                .execute(new CustomStringCallback() {
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onSuccessString(Gson gson, String responseBody) {
                        try {
                            JSONObject object = new JSONObject(responseBody);
                            if (object.getInt("result") == 200){
                                mIView.sendTuCaoSuccess(userId,object.getJSONObject("data").getInt("id"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void upLoadImage(int userId, int tucaoId, List<File> files){
        OkGo.<String>post(HttpUrl.LOAD_TUCAO_IMAGE_URL)
                .addFileParams("file",files)
                .params("userId",userId)
                .params("tucaoId",tucaoId)
                .execute(new CustomStringCallback() {
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onSuccessString(Gson gson, String responseBody) {
                        try {
                            JSONObject object = new JSONObject(responseBody);
                            if (object.getInt("result") == 200){
                                mIView.upLoadImageSuccess();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
